package com.caifeng.myapplication.login;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.caifeng.myapplication.R;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private Intent intent;
    private EditText phone, yzm;
    TextView hyzm, login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        phone = findViewById(R.id.in_phone);
        yzm = findViewById(R.id.in_yzm);
        hyzm = findViewById(R.id.h_yzm);
        login = findViewById(R.id.login);

        phone.addTextChangedListener(textWatcher);
        yzm.addTextChangedListener(textWatcher);
    }


    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, final int start, int before, int count) {

            if (phone.getEditableText().length() > 0) {
                hyzm.setOnClickListener(new View.OnClickListener() {
                    String a = genRandomNum();

                    @Override
                    public void onClick(View v) {
                        new AlertDialog.Builder(MainActivity.this)
                                .setTitle("验证码：")
                                .setMessage(a)
                                .setPositiveButton("确定", null).show();
                        login.setVisibility(View.VISIBLE);
                        CountTimerUtils mCountTimerUtils = new CountTimerUtils(30000, 1000, hyzm);
                        mCountTimerUtils.start();
                        login.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (yzm.getEditableText().length() == 0) {
                                    Toast.makeText(MainActivity.this, "请输入", Toast.LENGTH_SHORT).show();

                                } else if (a.equals(yzm.getText().toString())) {
                                    intent = new Intent(MainActivity.this, com.caifeng.myapplication.MainActivity.class);

                                    startActivity(intent);

                                } else {
                                    Toast.makeText(MainActivity.this, "请输入", Toast.LENGTH_SHORT).show();

                                }
                            }
                        });

                    }
                });
            }


        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };


    /**
     * 随机yzm
     */
    public String genRandomNum() {
        int maxNum = 10;
        int i;
        int count = 0;
        char[] str = {'1', '0', '2', '3', '4', '5', '6', '7', '8', '9'};
        StringBuffer pwd = new StringBuffer();
        Random r = new Random();
        while (count < 4) {
            i = Math.abs(r.nextInt(maxNum));
            if (i >= 0 && i < str.length) {
                pwd.append(str[i]);
                count++;
            }
        }

        return pwd.toString();
    }


}
